package com.coinland.study.util;

import android.text.TextUtils;

import java.util.Arrays;

/**
 * 类描述: 计算类
 * 创建人:  shenZ
 * 创建时间: 2020/3/26
 * 联系方式: 252079249@qq.com
 * 修改时间: 2020/3/26
 */
public class Calculate {

    /**
     * 查找最接近目标值的数，并返回下标区间
     *
     * @param array
     * @param targetNum
     * @return  int  下标值
     */
    public static int binarySearchKey(int[] array, int targetNum) {
        Arrays.sort(array);
        int left = 0, right;
        for (right = array.length - 1; left != right; ) {
            int midIndex = (right + left) / 2;
            int mid = (right - left);
            int midValue = array[midIndex];
            if (targetNum == midValue) {
                return midIndex;
            }
            if (targetNum > midValue) {
                left = midIndex;
            } else {
                right = midIndex;
            }
            if (mid <= 1) {
                break;
            }
        }
        int rightnum = ((Integer) array[right]).intValue();
        int leftnum = ((Integer) array[left]).intValue();
        //取较小的值
        return leftnum<rightnum? left:right;

    }

}
